import drawQrcode from '../../../../utils/weapp.qrcode.js'

var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
var app = getApp();

Page({
  data: {
     // 默认选中菜单
     currentTab: 0,
     index: 0,
     reagentId: '',
    categoryFilter: false,
    tmChemReagentEntity: [],
    categoryId: 0,
    text: 'https://m.baidu.com'
  },
  getTmChemReagentEntity() {
    var that = this;
    console.log('获取试剂信息详情')
    util.request(api.ChemreagentDetail, {reagentId: that.data.reagentId })
      .then(function (res) {
        if (res.code === 0) {
          that.setData({
            tmChemReagentEntity: res.tmChemReagentEntity
          });
        }
      });
  },
  draw () {
    var that = this;

    setTimeout(function () {
      drawQrcode({
        width: 160,
        height: 160,
        x: 20,
        y: 20,
        canvasId: 'myQrcode',
        // ctx: wx.createCanvasContext('myQrcode'),
        typeNumber: 10,
        text: app.globalData.qrAddr + that.data.reagentId,
        image: {
          dx: 70,
          dy: 70,
          dWidth: 60,
          dHeight: 60
        }
      })
    })
  },
  download () {
    // 导出图片
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 300,
      height: 300,
      destWidth: 300,
      destHeight: 300,
      canvasId: 'myQrcode',
      success(res) {
        console.log('图片的临时路径为：', res.tempFilePath)
        let tempFilePath = res.tempFilePath
        // 保存图片，获取地址
        // wx.saveFile({
        //   tempFilePath,
        //   success (res) {
        //     const savedFilePath = res.savedFilePath
        //     console.log('savedFilePath', savedFilePath)
        //   }
        // })

        // 保存到相册
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success: function (res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    that.setData({
      reagentId: options.reagentId
    })
    that.getTmChemReagentEntity();
    this.draw();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})